//
// Created by lenovo on 2024/7/2.
//
#include "./GameObj.h"
#include "../Msg/Message.h"
namespace Game::Obj{
    Player::Player() {
            position = Vector2f ({300,400});
            scale = Vector2f ({150,150});
            speed = 0.2 ;
            anima = Graphic::AnimationBuilder()
                    .addPath(IMG_RESOURCE+"player_left_0.png")
                    .addPath(IMG_RESOURCE+"player_left_1.png")
                    .addPath(IMG_RESOURCE+"player_left_2.png")
                    .addPath(IMG_RESOURCE+"player_left_3.png")
                    .addPath(IMG_RESOURCE+"player_left_4.png")
                    .addPath(IMG_RESOURCE+"player_left_5.png")
                    .position(position)
                    .interval(1)
                    .opacity(0.8f)
                    .loop(true)
                    .pause()
                    .play()
                    .scale(scale)
                    .build();
            collider = new AABB;
//            collider->setPosition(position + Vector2f({scale.x / 2 , scale.y / 2}));
//            collider->setScale(scale - Vector2f({90,40}));
            adjustCollider();
    }

    /**
     * @note
     * 由于图片与position有视觉偏差
     * 故需要调整collider使之与视觉上的实体重合
     */
    void Player::adjustCollider (){
        collider->setPosition(position + Vector2f({scale.x / 2 , scale.y / 2}));
        collider->setScale(
                Vector2f({
                    scale.x * 60 /  150 ,
                    scale.y * 110 / 150
                })
                );
    }

    void Player::setPosition(Vector2f p) {
        position = p ;
        anima->setPosition(p);
        adjustCollider();
    }

    void Player::setScale(Vector2f scale) {
        this->scale = scale;
        anima->setScale(scale);
        adjustCollider();
    }

    void Player::handleInput()  {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
            position.x -= speed;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
            position.x += speed;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
            position.y -= speed;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
            position.y += speed;
        }

        // Update animation and collider positions
        anima->setPosition(position);
        collider->setPosition(position + Vector2f({scale.x / 4 + 10, scale.y / 4}));
    }

    void Player::update(float delta) {
        handleInput();
        anima->update(delta);
//            drawCollider(*collider);
    }

    void Player::draw(sf::RenderWindow &window) {
        window.draw(*anima);
//        drawCollider(window , *collider);
    }

    void Player::onCollision(Game::Obj::GameObj *other) {
//        cout << "Player On Collision " << endl << endl << endl ;
//        setScale(scale + Vector2f({10,10}));
        if (auto* enemy = dynamic_cast<Enemy*>(other)){
            Msg::MsgBus::getInstance().post(
                    new Msg::DestroyMsg(this)
                    );
        }
    }

    Player::~Player(){
        delete anima ;
        delete collider ;
    }
}
